/**
 *
 */
package com.wedroid.r2d2.config;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import com.wedroid.r2d2.contact.ContactStore;
import com.wedroid.r2d2.contact.EhCacheContactStore;

/**
 * @author lute
 */
@Configuration
@EnableCaching
public class CacheContextConfig {

    /**
     * @return
     */
    @Bean(autowire = Autowire.BY_TYPE)
    public CacheManager cacheManager() {
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
        return cacheManager;
    }

    /**
     * @return
     */
    @Bean
    public EhCacheManagerFactoryBean ehcache() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        ehCacheManagerFactoryBean.setShared(true);
        return ehCacheManagerFactoryBean;
    }

    /**
     * @return
     */
    @Bean(autowire = Autowire.BY_TYPE)
    public ContactStore contactStore() {
        EhCacheContactStore contactStore = new EhCacheContactStore();
        EhCacheCacheManager cacheManager = (EhCacheCacheManager) cacheManager();
        contactStore.setEhCacheCacheManager(cacheManager);
        contactStore.setMaxCacheSize(2000);
        return contactStore;
    }

}
